input = [[1, 2, 2, 3, 5], [3, 2, 3, 4, 4], [2, 4, 5, 3, 1], [6, 7, 1, 4, 5], [5, 1, 1, 2, 4]]

m = len(input) - 1
n = len(input[0]) - 1

p = [[False for y in x] for x in input]
a = [[False for y in x] for x in input]


def dfs(i, j, graph, height):
    if i < 0 or j < 0 or i > m or j > n:
        return
    if graph[i][j]:
        return
    if input[i][j] < height:
        return
    graph[i][j] = True
    height = input[i][j]
    dfs(i - 1, j, graph, height)
    dfs(i + 1, j, graph, height)
    dfs(i, j - 1, graph, height)
    dfs(i, j + 1, graph, height)


for i in range(m + 1):
    dfs(i, 0, p, 0)
    dfs(i, n, a, 0)
for j in range(n + 1):
    dfs(0, j, p, 0)
    dfs(m, j, a, 0)

result = []
for i in range(m + 1):
    for j in range(n + 1):
        if a[i][j] and p[i][j]:
            result.append((i, j))

print(result)
